
var animation
var app = getApp();
var api = require('../../api.js');
var address;
Page({

  /*** 页面的初始数据**/
  data: {
    id:0,
    menuType: 0,
    begin: null,
    status: 1,
    end: null,
    isVisible: false,
    animationData: {},
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    contact_name:'',
    tel:'',
    address:'',
    is_default:0,
    payorder:false,
  },

  onLoad: function (options) {
    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
    // 默认联动显示北京
    
    this.getCityInfo();
    
    if(options){
      if(options.id){
        this.setData({
          id: options.id
        })
        this.addressInfo();
      }
      if (options.pay_order) {
        this.setData({
          payorder: true,
        })
      }
    }
  },
  getCityInfo(){
    var page =this;
    wx.showLoading();
    app.request({
      url: api.default.city,
      method: 'POST',
      data: {
      },
      success: function (res) {
        if (res.errcode == 0) {
          address = res.data;
          var id = address.provinces[0].id;
          page.setData({
            provinces: address.provinces,
            citys: address.citys[id],
            areas: address.areas[address.citys[id][0].id],
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
    
  },

  // 执行动画
  startAnimation: function (isShow, offset) {
    var that = this
    var offsetTem
    if (offset == 0) {
      offsetTem = offset
    } else {
      offsetTem = offset + 'rpx'
    }
    this.animation.translateY(offset).step()
    this.setData({
      animationData: this.animation.export(),
      isVisible: isShow
    })
    console.log(that.data)
  },
  // 选择状态按钮
  selectState: function (e) {
    console.log('selectState1')
    this.startAnimation(false, -200)
    var status = e.currentTarget.dataset.status
    this.setData({
      status: status
    })
    console.log(this.data)

  },
  // 日志选择
  bindDateChange: function (e) {
    console.log(e)
    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        begin: e.detail.value
      })
    } else if (e.currentTarget.dataset.type == 2) {
      this.setData({
        end: e.detail.value
      })
    }
  },
  sureDateTap: function () {
    this.data.pageNo = 1
    this.startAnimation(false, -200)
  },

  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    console.log('111111111')
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    console.log(isShow)
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(43 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
    that.setData({
      areaInfo: areaInfo,
    })
  },
  hideCitySelected: function (e) {
    console.log(e)
    this.startAddressAnimation(false)
  },
  // 处理省市县联动逻辑
  cityChange: function (e) {
    console.log(e)
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
    console.log(this.data)
  },
  //绑定输入
  inputContactName:function(e){
    this.setData({
      contact_name: e.detail.value.replace(/\s+/g, '')
    });
  },
  inputTel: function (e) {
    this.setData({
      tel: e.detail.value.replace(/\s+/g, '')
    });
  },
  inputAddress: function (e) {
    this.setData({
      address: e.detail.value.replace(/\s+/g, '')
    });
  },
  checkAll:function(e){
    var page= this
    if(page.data.is_default==0){
      page.setData({
        is_default:1
      })
    }else{
      page.setData({
        is_default: 0
      })
    }
  },
  //提交地址
  submitAddress:function(){
    var page = this;
    if (page.data.contact_name==''){
      wx.showToast({
        title: '请输入收货人姓名',
        icon:'none'
      })
      return false;
    }
    if (page.data.tel == '') {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
      return false;
    }
    if (page.data.address == '') {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })
      return false;
    }
    if (page.data.areaInfo == '') {
      wx.showToast({
        title: '请选择地区',
        icon: 'none'
      })
      return false;
    }
    wx.showLoading({
      title: '提交中',
    })
    app.request({
      url: api.shop.add_address,
      method: 'POST',
      data: {
        id: page.data.id == 0 ? '':page.data.id ,
        contact_name:page.data.contact_name,
        tel:page.data.tel,
        address:page.data.address,
        is_default: page.data.is_default,
        areaInfo:page.data.areaInfo
      },
      success: function (res) {
        if (res.errcode == 0) {
          if(page.data.payorder==true){
            let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
            let prevPage = pages[pages.length - 2];
            //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。
            prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
              address_id: res.data,
            })
          }
          wx.navigateBack({
            delta: 1
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  addressInfo:function(){
    var page =this
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.shop.address_info,
      method: 'POST',
      data: {
        id:page.data.id
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            address: res.data.address,
            tel: res.data.tel,
            contact_name: res.data.contact_name,
            is_default: res.data.is_default,
            areaInfo: res.data.province+','+res.data.city+','+res.data.area,
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  

})